package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.ReservaDao;
import edu.upc.clase.demo.entity.Reserva;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Emmanuel
 */
@Repository
public class ReservaDaoImpl extends SimpleJdbcDaoSupport implements ReservaDao {
    private static Logger log = LoggerFactory.getLogger(ReservaDaoImpl.class);
    
    @Autowired
    public ReservaDaoImpl(DataSource dataSource) 
    {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
     
    @Override
    public Integer insertar(Reserva reserva) 
    {
    try {
        getJdbcTemplate().update(
        "insert into reserva (cocliente, cosala, fefechareserva, fehorainicial, fehorafinal, sscostototal, flestado) values (?,?,?,?,?,?,?)",
        reserva.getCliente().getCocliente(), reserva.getSala().getCosala(),
        reserva.getFefechareserva(), reserva.getFehorainicial(), reserva.getFehorafinal(), 
        reserva.getSscostototal(), reserva.getFlestado());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
    }catch (Exception e) {
        log.error("error al insertar");
    }
    return null;
    }
    
    /*
    @Override
    public Integer insertar(Reserva reserva) {
        try {
            getJdbcTemplate().update(
                    "insert into reserva(nombreSala, fecha, idUsuario) "+ 
                    "values (?,?,?)",
                    reserva.getNombreSala(),
                    reserva.getFecha(),
                    reserva.getUsuario().getId());
            return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
        } catch (Exception e) {
            log.error("Error al insertar");
        }
        return null;
    }*/
    
    @Override
    public void actualizar(Reserva reserva) {
        getJdbcTemplate().update(
        "update reserva set fefechareserva = ?, fehorainicial = ?, fehorafinal = ?, sscostototal = ?, flestado= ? where coreserva = ?",
        reserva.getFefechareserva(), reserva.getFehorainicial(), reserva.getFehorafinal(), reserva.getSscostototal(), reserva.getFlestado());
    }

    @Override
    public void eliminar(Reserva reserva) {
        getJdbcTemplate().update(
                "delete from reserva where coreserva = ?", reserva.getCoreserva());
    }

    @Override
    public Reserva buscaReserva(Integer coreserva) {
    
    try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select * from reserva where coreserva = ?",
                    new BeanPropertyRowMapper<Reserva>(Reserva.class), coreserva);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    
    }

}
